﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FrmMain.cs" company="EarthImitate">
//   All rights reserved by lamemmv
// </copyright>
// <summary>
//   Defines the FrmMain type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace EarthImitate
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    /// <summary>
    /// The FrmMain
    /// </summary>
    public partial class FrmMain : Form
    {
        #region Local variables

        /// <summary>
        /// The bitmap
        /// </summary>
        private Bitmap bitmap;
        /// <summary>
        /// The graphics
        /// </summary>
        private Graphics graphics;
        int curentPoint = 0;      
        Earth earth;
        Sun sun;
        Orbit orbit;
        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="FrmMain"/> class.
        /// </summary>
        public FrmMain()
        {
            InitializeComponent();
            this.Initialize();
        }

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        private void Initialize()
        {
            Rectangle rec = this.ClientRectangle;
            this.bitmap = new Bitmap(rec.Width, rec.Height);
            this.graphics = Graphics.FromImage(this.bitmap);
            this.graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

           
            Point2D p0 = new Point2D();
            p0.x = this.ClientSize.Width / 2 ;
            p0.y = this.ClientSize.Height / 2 ;

            earth = new Earth(p0);
            sun = new Sun(p0);
            orbit = new Orbit(p0);

            orbit.Draw(this.graphics);
            earth.Draw(this.graphics);
            sun.Draw(this.graphics);
           
        }

        #endregion

      
        #region Events

      

        /// <summary>
        /// Handles the KeyDown event of the FrmMain control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.KeyEventArgs"/> instance containing the event data.</param>
        private void FrmMain_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Space:
                    if (timerOrbit.Enabled == false)
                        timerOrbit.Start();
                    else timerOrbit.Stop();

                    if (timerEarth.Enabled == false)
                        timerEarth.Start();
                    else timerEarth.Stop();
                    break;

                case Keys.Escape:
                    timerOrbit.Stop();
                    timerEarth.Stop();
                    this.Close();
                    break;
                case Keys.F1:
                    if (timerOrbit.Enabled == false)
                        timerOrbit.Start();
                    else timerOrbit.Stop();
                    break;
                case Keys.F2:
                    if (timerEarth.Enabled == false)
                        timerEarth.Start();
                    else timerEarth.Stop();
                    break;
            }           
        }


        private void timerOrbit_Tick(object sender, EventArgs e)
        {
            
            this.graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            this.graphics.Clear(this.BackColor);
            orbit.Draw(this.graphics);
            sun.Draw(this.graphics);
           

            if (curentPoint == orbit.WireFrame.NumVertex - 1)
                curentPoint = 0;
            Point2D p1 = Drawing.chieuPCoxy(orbit.WireFrame.Vertexes[curentPoint], 500);
            earth.Move(p1.x + orbit.GocToaDo.x, p1.y + orbit.GocToaDo.y);
            earth.Draw(this.graphics);
            if (timerEarth.Enabled == false)
            {
                curentPoint++;
            }
            Invalidate();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            timerOrbit.Enabled = true;
            timerOrbit.Start();
            timerEarth.Enabled = true;
            timerEarth.Start();
        }

        private void FrmMain_ClientSizeChanged(object sender, EventArgs e)
        {
            Rectangle r = this.ClientRectangle;
            bitmap = new Bitmap(r.Width, r.Height);
            this.graphics = Graphics.FromImage(bitmap);
            Invalidate();
        }

        private void timerEarth_Tick(object sender, EventArgs e)
        {
            if (timerOrbit.Enabled == false)
            {
                this.graphics.Clear(this.BackColor);
                orbit.Draw(this.graphics);
                sun.Draw(this.graphics);
                earth.Draw(this.graphics);
            }
            else
            {
                curentPoint++;
            }
            earth.Turn();
            this.Invalidate();
        }
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            Graphics g1 = e.Graphics;
            g1.DrawImageUnscaled(this.bitmap, 0, 0);
        }
        #endregion

    }
}
